﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VideoStream.Model;


/// <summary>
/// 标记控制
/// </summary>
/// <param name="width">画面宽度</param>
/// <param name="height">画面高度</param>
/// <param name="sX">标记起点X</param>
/// <param name="sY">标记起点Y</param>
/// <param name="eX">标记终点X</param>
/// <param name="eY">标记终点Y</param>
/// <param name="pT">标记类型
/// 1 = 画直线    3 = 画空心矩形    5 = 空心圆    10 = 箭头
/// </param>
/// <param name="color">标记颜色
/// #ff7200    #9943e3    #24def4    #07df8b    #288fe7    #ffe404     #ef3030     #000000
/// 橙色       紫色       青色       绿色       蓝色       黄色        红色        黑色
/// </param>
/// <param name="pWidth">标记线宽
/// 1,2,4,6
/// </param>

namespace VideoStream.CustomControl
{
    public partial class MarkOptionPanel : UserControl
    {

        private List<PictureButton> group1 => table1.Controls.OfType<PictureButton>().ToList();
        private List<PictureButton> group2 => table2.Controls.OfType<PictureButton>().ToList();
        private List<PictureButton> group3 => table3.Controls.OfType<PictureButton>().ToList();

        private int geoType = 3;
        private string colorType = "#ef3030";
        private int solidType = 1;

        public int GeoType
        {
            get { return geoType == 0 ? 3 : geoType; }
            set
            {
                geoType = value;
            }
        }
        public string ColorType
        {
            get { return string.IsNullOrEmpty(colorType) ? "#ef3030" : colorType; }
            set
            {
                colorType = value;
            }
        }
        public int SolidType
        {
            get { return solidType == 0 ? 1 : solidType; }
            set
            {
                solidType = value;
            }
        }
        public new event EventHandler Click
        {
            add
            {
                base.Click += value;
                group1.ForEach(o => o.Click += value);
                group2.ForEach(o => o.Click += value);
                group3.ForEach(o => o.Click += value);
            }
            remove
            {
                base.Click -= value;
                group1.ForEach(o => o.Click -= value);
                group2.ForEach(o => o.Click -= value);
                group3.ForEach(o => o.Click -= value);
            }
        }

        public event EventHandler ClearClick
        {
            add { btn_Clear.Click += value; }
            remove { btn_Clear.Click -= value; }
        }



        public MarkOptionPanel()
        {
            InitializeComponent();

            //group1.AddRange(table1.Controls.OfType<PictureButton>());
            //group2.AddRange(table2.Controls.OfType<PictureButton>());
            //group3.AddRange(table3.Controls.OfType<PictureButton>());

            group1.Where(o => o != btn_Clear).ToList().ForEach(o =>
                {
                    o.Click += Geo_Click;
                });
            group2.ForEach(o =>
            {
                o.Click += Solid_Click;
            });
            group3.ForEach(o =>
            {
                o.Click += Color_Click;
            });

            btn_Cube.Tag = 3;
            btn_Cricle.Tag = 5;
            btn_Arrow.Tag = 10;
            btn_Clear.Tag = -1;

            btn_s1.Tag = 1;
            btn_s2.Tag = 2;
            btn_s3.Tag = 4;
            btn_s4.Tag = 6;

            btn_r.Tag = "#ef3030";
            btn_y.Tag = "#ffe404";
            btn_g.Tag = "#07df8b";
            btn_p.Tag = "#9943e3";
            btn_b.Tag = "#288fe7";
            btn_a.Tag = "#24def4";
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            Pen p = new Pen(TVColor.Color_HighLight, 1);
            g.DrawRectangle(p, 0, 0, this.Width - p.Width, this.Height - p.Width);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            var gT = group1.Where(o => (int)o.Tag == GeoType)?.FirstOrDefault();
            Geo_Click(gT, null);
            var sT = group2.Where(o => (int)o.Tag == SolidType)?.FirstOrDefault();
            Solid_Click(sT, null);
            var cT = group3.Where(o => (string)o.Tag == ColorType)?.FirstOrDefault();
            Color_Click(cT, null);
        }


        private void Color_Click(object sender, EventArgs e)
        {
            PictureButton pb = sender as PictureButton;
            pb.Checked = true;
            if (pb.ButtonType == ButtonType.Radio)
                group3.Where(o => o != pb).ToList().ForEach(o => o.Checked = false);
            ColorType = pb.Tag as string;
        }

        private void Solid_Click(object sender, EventArgs e)
        {
            PictureButton pb = sender as PictureButton;
            pb.Checked = true;
            if (pb.ButtonType == ButtonType.Radio)
                group2.Where(o => o != pb).ToList().ForEach(o => o.Checked = false);
            SolidType = (int)pb.Tag;
        }

        private void Geo_Click(object sender, EventArgs e)
        {
            PictureButton pb = sender as PictureButton;
            pb.Checked = true;
            if (pb.ButtonType == ButtonType.Radio)
                group1.Where(o => o != pb).ToList().ForEach(o => o.Checked = false);
            GeoType = (int)pb.Tag;
        }

    }
}
